package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.xmall.bean.table.*;
import com.xmall.bean.vo.ListDataVo;
import com.xmall.mapper.*;
import com.xmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public ListDataVo<User> queryUserList(Integer page, Integer limit, String username,
                                        String mobile, String sort, String order) {
        PageHelper.startPage(page, limit);//修改预编译语句 //分页
        UserExample userExample = new UserExample();// userExample工具对象，用来封装所有查询相关的数据
        userExample.setOrderByClause(sort + " " + order);//结果排序规则
        UserExample.Criteria criteria = userExample.createCriteria();//设置查询语句 查询条件where
        if(username != null && username.length() != 0){
            criteria.andUsernameLike("%" + username + "%");
        }
        if(mobile != null && username.length() != 0){
            criteria.andMobileEqualTo(mobile);
        }
        //把数据封装到
        ListDataVo<User> listDataVo = new ListDataVo<>();
        List<User> users = userMapper.selectByExample(userExample);
        listDataVo.setItems(users);
        PageInfo<User> userPageInfo = new PageInfo<>(users);//查询数据总条数
        long total = userPageInfo.getTotal();//没有分页的情况下的 总的数据量
        listDataVo.setTotal((int) total);
        return listDataVo;
    }





    @Override
    public ListDataVo<Address> queryAddressList(Integer page, Integer limit, String name,
                                              Integer userId, String sort, String order) {


        //分页工具分页
        //page页码 limit当前页的大小
        PageHelper.startPage(page, limit);//修改我们的预编译sql语句//分页

        //AddressExample用来 封装 所有查询相关的数据
        AddressExample addressExample = new AddressExample();//cskaoyanmall_address表查询实例工具
        addressExample.setOrderByClause(sort + " " + order);//设置结果排序规则

        AddressExample.Criteria criteria = addressExample.createCriteria();//设置查询语句 查询条件where
        if(name != null){
            criteria.andNameLike("%" + name + "%");//模糊查询
        }
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        //将查询结果封装给MallDate<T>中
        ListDataVo<Address> listDataVo = new ListDataVo<>();
        //XXXMapper 执行 XXX 相关查询
        List<Address> addresses = addressMapper.selectByExample(addressExample);//执行查询语句
        listDataVo.setItems(addresses);

        //PageInfo工具得到查询结果的总条数total
        PageInfo<Address> addressPageInfo = new PageInfo<Address>(addresses);
        long total = addressPageInfo.getTotal();//得到总条数total
        listDataVo.setTotal((int) total);//将total封装到ListDataVo中
        return listDataVo;//返回mallDate
    }

    @Override
    public ListDataVo<Collect> queryCollectList(Integer page, Integer limit, Integer userId,
                                              Integer valueId, String sort, String order) {

        /*第一部分：设置查询语句并封装到collectExample*/
        PageHelper.startPage(page, limit);//分页
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort + " " + order);//排序  注意中间有个空格 否则会报错500

        CollectExample.Criteria criteria = collectExample.createCriteria();//设置查询语句
        if(userId != null){
            criteria.andUserIdEqualTo(userId);//精确查询
        }
        if(valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }

        /*第二部分：执行查询获得结果并封装返回*/

        //获得查询结果collects
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //封装collets
        ListDataVo<Collect> listDataVo = new ListDataVo<>();
        listDataVo.setItems(collects);
        //查询total
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        //封装total
        listDataVo.setTotal((int) total);
        //返回ListDataVo
        return listDataVo;
    }

    @Override
    public ListDataVo<Footprint> queryFootprintList(Integer page, Integer limit, Integer userId,
                                                  Integer goodsId, String sort, String order) {
        //设置查询
        PageHelper.startPage(page, limit);
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(sort + " " + order);

        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(goodsId != null){
            criteria.andGoodsIdEqualTo(goodsId);
        }

        //执行查询
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);

        ListDataVo<Footprint> listDataVo = new ListDataVo<>();
        listDataVo.setItems(footprints);

        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        listDataVo.setTotal((int) total);
        return listDataVo;
    }

    @Override
    public ListDataVo<SearchHistory> queryHistoryList(Integer page, Integer limit, Integer userId,
                                                    String keyword, String sort, String order) {

        PageHelper.startPage(page, limit);

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(sort + " " + order);

        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword != null){
            criteria.andKeywordLike(keyword);
        }

        //查询结果封装给ListDataVo并返回
        ListDataVo<SearchHistory> listDataVo = new ListDataVo<>();

        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        listDataVo.setItems(searchHistories);

//        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        listDataVo.setTotal((int) total);

        return listDataVo;

    }

    @Override
    public ListDataVo<Feedback> queryFeedbackList(Integer page, Integer limit, String username,
                                                String sort, String order, Integer id) {
        PageHelper.startPage(page, limit);
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.setOrderByClause(sort + " " + order);
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if(username != null){
            criteria.andUsernameEqualTo(username);
        }
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        //查询封装
        ListDataVo<Feedback> listDataVo = new ListDataVo<>();
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        listDataVo.setItems(feedbacks);

        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        long total = feedbackPageInfo.getTotal();
        listDataVo.setTotal((int) total);
        return listDataVo;
    }


}
